<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/hexo_blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo_blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo_blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo_blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo_blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bone-nails.gitee.io","root":"/hexo_blog/","images":"/hexo_blog/images","scheme":"Gemini","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/hexo_blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":true}};
  </script>
<meta name="description" content="Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell只作基础入门，没有深入探究。">
<meta property="og:type" content="article">
<meta property="og:title" content="初识Shell脚本">
<meta property="og:url" content="https://bone-nails.gitee.io/2021/04/01/%E5%88%9D%E8%AF%86Shell%E8%84%9A%E6%9C%AC/index.html">
<meta property="og:site_name" content="Carson Zhao">
<meta property="og:description" content="Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell只作基础入门，没有深入探究。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-01T04:23:20.000Z">
<meta property="article:modified_time" content="2021-04-01T04:21:47.403Z">
<meta property="article:author" content="Carson Zhao">
<meta property="article:tag" content="Shell">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://bone-nails.gitee.io/2021/04/01/%E5%88%9D%E8%AF%86Shell%E8%84%9A%E6%9C%AC/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>初识Shell脚本 | Carson Zhao</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/hexo_blog/atom.xml" title="Carson Zhao" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo_blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Carson Zhao</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/hexo_blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-about"><a href="/hexo_blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-tags"><a href="/hexo_blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/hexo_blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Shell%E7%9A%84%E7%A7%8D%E7%B1%BB%E6%9C%89%E5%BE%88%E5%A4%9A%EF%BC%8C%E8%BF%99%E9%87%8C%E5%B0%86%E8%A6%81%E4%BB%8B%E7%BB%8D%E7%9A%84%E6%98%AFBash%EF%BC%8C%E4%B9%9F%E6%98%AF%E5%A4%A7%E5%A4%9A%E6%95%B0Linux%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8%E7%9A%84Shell"><span class="nav-number">1.</span> <span class="nav-text">Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AA%E4%BD%9C%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8%EF%BC%8C%E6%B2%A1%E6%9C%89%E6%B7%B1%E5%85%A5%E6%8E%A2%E7%A9%B6%E3%80%82"><span class="nav-number">2.</span> <span class="nav-text">只作基础入门，没有深入探究。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8Bshell"><span class="nav-number"></span> <span class="nav-text">查看shell</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E5%BD%93%E5%89%8Dlinux%E7%B3%BB%E7%BB%9F%E4%BD%BF%E7%94%A8%E7%9A%84shell"><span class="nav-number"></span> <span class="nav-text">查看当前linux系统使用的shell</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%A9%E5%B1%95%E5%90%8D"><span class="nav-number"></span> <span class="nav-text">扩展名</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E8%BF%90%E8%A1%8C%E8%84%9A%E6%9C%AC%EF%BC%88%E5%81%87%E8%AE%BEtext-sh%EF%BC%89"><span class="nav-number"></span> <span class="nav-text">如何运行脚本（假设text.sh）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E5%BC%80%E5%A4%B4"><span class="nav-number"></span> <span class="nav-text">脚本开头</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#shell%E8%84%9A%E6%9C%AC%E5%B8%B8%E7%94%A8"><span class="nav-number"></span> <span class="nav-text">shell脚本常用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#echo"><span class="nav-number">1.</span> <span class="nav-text">echo</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#read%E4%BB%8Estdin%E8%8E%B7%E5%8F%96%E8%BE%93%E5%85%A5"><span class="nav-number">2.</span> <span class="nav-text">read从stdin获取输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E5%8F%98%E9%87%8F"><span class="nav-number">3.</span> <span class="nav-text">shell变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">4.</span> <span class="nav-text">shell字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E6%95%B0%E7%BB%84"><span class="nav-number">5.</span> <span class="nav-text">shell数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E9%87%8A"><span class="nav-number">6.</span> <span class="nav-text">注释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%AE%8A%E5%8F%98%E9%87%8F%EF%BC%88%E6%AF%94%E8%BE%83%E5%A4%8D%E6%9D%82%EF%BC%8C%E4%B8%8D%E5%81%9A%E4%BB%8B%E7%BB%8D%EF%BC%89"><span class="nav-number">7.</span> <span class="nav-text">特殊变量（比较复杂，不做介绍）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%9B%E5%88%99%E8%BF%90%E7%AE%97"><span class="nav-number">8.</span> <span class="nav-text">四则运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#if-else%E5%91%BD%E4%BB%A4"><span class="nav-number">9.</span> <span class="nav-text">if else命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#case-esac%E8%AF%AD%E5%8F%A5"><span class="nav-number">10.</span> <span class="nav-text">case esac语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="nav-number">11.</span> <span class="nav-text">for循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#while%E5%BE%AA%E7%8E%AF"><span class="nav-number">12.</span> <span class="nav-text">while循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#until%E5%BE%AA%E7%8E%AF"><span class="nav-number">13.</span> <span class="nav-text">until循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E5%87%BD%E6%95%B0"><span class="nav-number">14.</span> <span class="nav-text">shell函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB"><span class="nav-number">15.</span> <span class="nav-text">文件包含</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Carson Zhao"
      src="/hexo_blog/images/1.png">
  <p class="site-author-name" itemprop="name">Carson Zhao</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo_blog/archives">
          <span class="site-state-item-count">59</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo_blog/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo_blog/tags/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/hexo_blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bone-nails.gitee.io/2021/04/01/%E5%88%9D%E8%AF%86Shell%E8%84%9A%E6%9C%AC/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo_blog/images/1.png">
      <meta itemprop="name" content="Carson Zhao">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Carson Zhao">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          初识Shell脚本
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-04-01 12:23:20 / 修改时间：12:21:47" itemprop="dateCreated datePublished" datetime="2021-04-01T12:23:20+08:00">2021-04-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/hexo_blog/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>2 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell"><a href="#Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell" class="headerlink" title="Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell"></a>Shell的种类有很多，这里将要介绍的是Bash，也是大多数Linux系统默认使用的Shell</h3><h3 id="只作基础入门，没有深入探究。"><a href="#只作基础入门，没有深入探究。" class="headerlink" title="只作基础入门，没有深入探究。"></a>只作基础入门，没有深入探究。</h3><span id="more"></span>
<h2 id="查看shell"><a href="#查看shell" class="headerlink" title="查看shell"></a>查看shell</h2><p><code>cat /etc/shells</code></p>
<h2 id="查看当前linux系统使用的shell"><a href="#查看当前linux系统使用的shell" class="headerlink" title="查看当前linux系统使用的shell"></a>查看当前linux系统使用的shell</h2><p><code>echo $SHELL</code></p>
<h2 id="扩展名"><a href="#扩展名" class="headerlink" title="扩展名"></a>扩展名</h2><p>一般扩展为<code>.sh</code>，如果用php写的脚本也可以用<code>.php</code>作为扩展。事实上扩展名可以是任意的，不影响脚本的执行。使用<code>.sh</code>只是出于规范。</p>
<h2 id="如何运行脚本（假设text-sh）"><a href="#如何运行脚本（假设text-sh）" class="headerlink" title="如何运行脚本（假设text.sh）"></a>如何运行脚本（假设text.sh）</h2><ol>
<li>cd到当前脚本所在路径，执行命令<code>sh  text.sh</code>或者<code>/bin/sh text.sh</code>,此方式就不需要在第一行指定解释器信息。</li>
<li>cd到当前脚本所在路径，赋予脚本执行权限<code>chmod +x ./text.sh</code>,执行脚本<code>./text.sh</code>。这里的./代表当前目录下<h2 id="脚本开头"><a href="#脚本开头" class="headerlink" title="脚本开头"></a>脚本开头</h2>每个shell脚本开头有一句<code>#!/bin/bash</code>,这里的<code>#！</code>是告诉系统<code>/bin/bash</code>指定的程序时解释此脚本文件的shell程序。<h2 id="shell脚本常用"><a href="#shell脚本常用" class="headerlink" title="shell脚本常用"></a>shell脚本常用</h2><h3 id="echo"><a href="#echo" class="headerlink" title="echo"></a>echo</h3></li>
<li><code>echo &quot;hello!&quot;</code>  用于向窗口输出文本，试了一下加不加双引号结果都一样。</li>
<li>echo的两种格式：通过PS1和PS2控制<ol>
<li><code>echo $PS1   [\u@\h \W]\$</code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;www.baidu.com&quot;</span><br><span class="line">www.baidu.com</span><br></pre></td></tr></table></figure></li>
<li><code>echo $PS2   &gt;</code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;</span><br><span class="line">&gt; www</span><br><span class="line">&gt; .</span><br><span class="line">&gt; baidu.com</span><br><span class="line">&gt; &quot;</span><br><span class="line">结果：</span><br><span class="line">www</span><br><span class="line">.</span><br><span class="line">baidu.com</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li>格式2中，echo遇到第一个“认为是不玩中的附加信息，继续等待用户输入，直到遇到第二个”，用&gt;作为提示符<h3 id="read从stdin获取输入"><a href="#read从stdin获取输入" class="headerlink" title="read从stdin获取输入"></a>read从stdin获取输入</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;bash</span><br><span class="line">echo &quot;what&#39;s your name?&quot;</span><br><span class="line">read NAME</span><br><span class="line">echo &quot;hello,$NAME&quot;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>通过read获取用户输入</p>
</blockquote>
</li>
</ol>
</li>
</ol>
<h3 id="shell变量"><a href="#shell变量" class="headerlink" title="shell变量"></a>shell变量</h3><ol>
<li>定义变量：<ol>
<li>格式：变量名=字符串/整数…..例：<code>A=&quot;good&quot;</code></li>
<li>等号两边不能有空格</li>
<li>变量名同标识符命名规则一样。</li>
</ol>
</li>
<li>将命令的结果赋值给变量：<ol>
<li>格式1：VARIABLE1=<code>pwd</code></li>
<li>格式2：VARIABLE2=$(pwd)</li>
<li>注意是反引号，为了不混淆，建议第二种</li>
</ol>
</li>
<li>使用变量：<ol>
<li>在变量名前加上<code>$</code></li>
<li>例1：<code>echo $A</code></li>
<li>例2：加上花括号，识别变量界限：<code>echo &quot;zzj is a &#123;$A&#125;man&quot;</code>这样系统就不会错认为$Aman<ol>
<li>试了一下，其实加上空格也可以起到{}的作用:<code>echo &quot;zzj is a $A man</code></li>
</ol>
</li>
<li>建议给所有使用变量加上{}</li>
</ol>
</li>
<li>只读变量（readonly）：<ol>
<li>直接赋值：<code>readonly A=&quot;good&quot;</code>A的值不能改变</li>
</ol>
</li>
<li>删除变量（unset）<ol>
<li>unset A</li>
<li>注意删除变量，变量名不用加$</li>
<li>注意只读变量不能被删除</li>
</ol>
</li>
<li>变量类型<ol>
<li>局部变量：只在当前shell生效</li>
<li>环境变量：调用系统环境变量保证程序运行</li>
<li>shell变量：待定<h3 id="shell字符串"><a href="#shell字符串" class="headerlink" title="shell字符串"></a>shell字符串</h3></li>
</ol>
</li>
<li>shell脚本字符串可用单/双引号，也可以不用引号</li>
<li>单引号：<code>A=&#39;good&#39;</code><ol>
<li>单引号里的任意字符都会原样输出，里面的特殊符号会失效。如<code>echo &#39;$A&#39;</code>结果就为<code>$A</code></li>
</ol>
</li>
<li>双引号：<code>A=&quot;good&quot;</code><ol>
<li>双引号里会保留特殊符号的含义</li>
</ol>
</li>
<li>转义字符 <code>\</code><ol>
<li>转义字符可以帮助我们在双引号里使一些特殊符号失效。如<code>echo &quot;zzj is a \&quot;$A\&quot; man&quot;</code>,使得双引号里的双引号失去作用</li>
<li>如何使转义字符生效：添加<code>-e</code>参数，如<code>echo &quot;zzj is a \&quot;good\&quot; man \n&quot;</code></li>
<li>显示不换行：<code>echo &quot;OK! \c&quot;</code></li>
</ol>
</li>
<li>获取字符串长度<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo $&#123;#A&#125;</span><br></pre></td></tr></table></figure></li>
<li>提取字符串<ol>
<li>从字符串第二个字符开始截取到第五个字符：<code>echo $(A:1:5)</code><h3 id="shell数组"><a href="#shell数组" class="headerlink" title="shell数组"></a>shell数组</h3></li>
</ol>
</li>
<li>一般形式：<code>array=(v0 v1 v2 v3 v4)</code><ol>
<li>用括号表示</li>
<li>元素用空格隔开</li>
</ol>
</li>
<li>定义单个分量：<code>array[0]=v0</code></li>
<li>读取数组：<code>$&#123;数组名[下标]&#125;</code>如<code>A=$&#123;array[0]&#125;</code><ol>
<li>使用@获取所有元素：<code>A=$&#123;array[@]&#125;</code></li>
</ol>
</li>
<li>获取数组长度（同获取字符串长度）<ol>
<li><code>length=$(#array[@])</code><h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3></li>
</ol>
</li>
<li>单行：#</li>
<li>多行：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">:&lt;&lt;EOF</span><br><span class="line"> ......</span><br><span class="line">EOF</span><br><span class="line">或者</span><br><span class="line">:&lt;&lt;!</span><br><span class="line">......</span><br><span class="line">!</span><br></pre></td></tr></table></figure>
<h3 id="特殊变量（比较复杂，不做介绍）"><a href="#特殊变量（比较复杂，不做介绍）" class="headerlink" title="特殊变量（比较复杂，不做介绍）"></a>特殊变量（比较复杂，不做介绍）</h3><h3 id="四则运算"><a href="#四则运算" class="headerlink" title="四则运算"></a>四则运算</h3></li>
<li><code>$(())</code>: 例：A=$((1+1))</li>
<li><code>$[]</code>: 例：A=$[1+1]</li>
<li><code>expr</code>:不用</li>
<li><code>let</code>: 例：let A=1+1<h3 id="if-else命令"><a href="#if-else命令" class="headerlink" title="if else命令"></a>if else命令</h3></li>
<li>格式1：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">if [  ]</span><br><span class="line">then</span><br><span class="line">   ......</span><br><span class="line">fi</span><br></pre></td></tr></table></figure></li>
<li>格式2：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">if [  ]</span><br><span class="line">then</span><br><span class="line">   ......</span><br><span class="line">elif [ ]</span><br><span class="line">then</span><br><span class="line">   ......</span><br><span class="line">else</span><br><span class="line">   ......</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
举例：<code>if [$A==$B]</code>,<code>if [$A -gt $B]</code>,<code>if [$A -lt $B]</code><h3 id="case-esac语句"><a href="#case-esac语句" class="headerlink" title="case esac语句"></a>case esac语句</h3>格式：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">read 值</span><br><span class="line">case 值 in</span><br><span class="line">模式1）</span><br><span class="line">   command1</span><br><span class="line">   command2</span><br><span class="line">   command3</span><br><span class="line">   ::</span><br><span class="line">模式2）</span><br><span class="line">   command1</span><br><span class="line">   command2</span><br><span class="line">   command3</span><br><span class="line">   ::</span><br><span class="line">*）</span><br><span class="line">   command1</span><br><span class="line">   command2</span><br><span class="line">   command3</span><br><span class="line">   ::</span><br><span class="line">esac</span><br><span class="line">::相当于break</span><br></pre></td></tr></table></figure>
<h3 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h3></li>
<li>格式1：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for loop in 1 2 3 4 5</span><br><span class="line">do</span><br><span class="line">   ......</span><br><span class="line">done</span><br></pre></td></tr></table></figure></li>
<li>格式2：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for loop in &#123;1..5&#125;</span><br><span class="line">do</span><br><span class="line">   ......</span><br><span class="line">done</span><br></pre></td></tr></table></figure></li>
<li>显示home目录下以.bash开头的文件<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for FILE in $HOME&#x2F;.bash*</span><br><span class="line">do</span><br><span class="line">   echo $FILE</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3></li>
<li>格式：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">while condition</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure></li>
<li>例：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">   K&#x3D;4-I</span><br><span class="line">    while (($K&gt;0))</span><br><span class="line">	do</span><br><span class="line">	   printf &quot;*&quot;</span><br><span class="line">	   ((K--))</span><br><span class="line">	done</span><br><span class="line">我试了一下K加不加$好像都没有影响。</span><br></pre></td></tr></table></figure>
<h3 id="until循环"><a href="#until循环" class="headerlink" title="until循环"></a>until循环</h3>格式：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">until condition</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="shell函数"><a href="#shell函数" class="headerlink" title="shell函数"></a>shell函数</h3>直接上例子吧：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">demoFun()&#123;</span><br><span class="line">    echo &quot;这是我的第一个 shell 函数!&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="文件包含"><a href="#文件包含" class="headerlink" title="文件包含"></a>文件包含</h3>假设我又1.sh和2.sh两个脚本，如何在2.sh中引用1.sh?</li>
<li>使用 . 号来引用test1.sh 文件<br><code>. ./test1.sh</code></li>
<li><code>source ./test1.sh</code></li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Carson Zhao
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://bone-nails.gitee.io/2021/04/01/%E5%88%9D%E8%AF%86Shell%E8%84%9A%E6%9C%AC/" title="初识Shell脚本">https://bone-nails.gitee.io/2021/04/01/初识Shell脚本/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/hexo_blog/tags/Shell/" rel="tag"># Shell</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/hexo_blog/2021/04/01/%E5%9F%BA%E4%BA%8ECentOS7%E6%90%AD%E5%BB%BAjsp%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83%E3%80%82/" rel="prev" title="基于CentOS7搭建jsp运行环境。">
                  <i class="fa fa-chevron-left"></i> 基于CentOS7搭建jsp运行环境。
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/hexo_blog/2021/04/01/vue%E7%AC%94%E8%AE%B0/" rel="next" title="vue笔记">
                  vue笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Carson Zhao</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">125k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:54</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/hexo_blog/js/utils.js"></script><script src="/hexo_blog/js/next-boot.js"></script>

  
<script src="/hexo_blog/js/local-search.js"></script>



<script>
if (document.querySelectorAll('.pdf-container').length) {
  NexT.utils.getScript('https://cdn.jsdelivr.net/npm/pdfobject@2.2.4/pdfobject.min.js', () => {
    document.querySelectorAll('.pdf-container').forEach(element => {
      PDFObject.embed(element.dataset.target, element, {
        pdfOpenParams: {
          navpanes : 0,
          toolbar  : 0,
          statusbar: 0,
          pagemode : 'thumbs',
          view     : 'FitH'
        },
        PDFJS_URL: '/hexo_blog/lib/pdf/web/viewer.html',
        height   : element.dataset.height
      });
    });
  }, window.PDFObject);
}
</script>


  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>

  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





<script src="/hexo_blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/hexo_blog/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":200,"height":400},"mobile":{"show":true},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
